extends Node2D

@onready var level_box: Node2D = $LevelBox
@onready var point_label: Label = $Ui/VBoxContainer/PointLabel

var point := 0:
	set(v):
		point = v
		change_point.rpc(v)

const SERVER_PORT = 10086

var peer : ENetMultiplayerPeer

@rpc("any_peer", "call_local")
func change_point(v: int) -> void:
	point_label.text = str(v)


func _on_host_button_button_down() -> void:
	if not peer:
		peer = ENetMultiplayerPeer.new()
		peer.create_server(SERVER_PORT)
		multiplayer.multiplayer_peer = peer
		start_game()


func start_game() -> void:
	if multiplayer.is_server():
		change_level(preload("res://demo/multi/level_test.tscn"))


func change_level(scene: PackedScene) -> void:
	for c in level_box.get_children():
		#level_box.remove_child(c)
		c.queue_free()
	level_box.add_child(scene.instantiate())


func _on_join_button_button_down() -> void:
	if not peer:
		peer = ENetMultiplayerPeer.new()
		peer.create_client("127.0.0.1", SERVER_PORT)
		multiplayer.multiplayer_peer = peer
		start_game()

